import { defaultRequest } from '@shared/utils/request/default-request';
import { ClientAppLoginReq } from './type';
export * from './type';

export function clientAppLoginApi(data: ClientAppLoginReq) {
  return defaultRequest.post<any>(
    {
      url: '/api/auth/admin',
      data
    },
    {
      includeToken: false,
      returnAxiosResponseAfterBusiness: true
    }
  );
}

/**
 * 刷新过期Token
 * @param data
 * @returns
 */
export function refreshTokenApi() {
  return defaultRequest.post<any>(
    {
      url: '/api/auth/refresh'
    },
    {
      returnAxiosResponseAfterBusiness: true
    }
  );
}

// Token 过期检查
export function isTokenExpired(token: string): boolean {
  try {
    const { exp } = JSON.parse(atob(token.split('.')[1]));
    return Date.now() >= exp * 1000 - 30000; // 提前30秒视为过期
  } catch {
    return true; // 解析失败视为过期
  }
}
